package com.moleq.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.moleq.base.OnResultListener;
import com.moleq.base.SmartBeep;
import com.moleq.logic.SaleList;
import com.moleq.mgpos.widget.SaleListItemViewCache;
import com.moleq.models.SaleListItem;
import com.moleq.mgpos.activities.R;
import com.moleq.util.BigDecmialTool;
import com.moleq.util.MoleQUtil;

public class SaleListViewAdapter extends BaseAdapter {

//	private int mSelectIndex = -1;
	private SaleList mData = new SaleList();
	private Context mContext;
	private LayoutInflater mInflater;
	private boolean mIsShowControls = false;
	private ListView mListView;

	public SaleListViewAdapter(Context context) {
		mContext = context;
		
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return mData.getItemCount();
	}

	@Override
	public Object getItem(int arg0) {
		return mData.getItem(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		SaleListItemViewCache viewCache;
		View rowView = convertView;

		if (rowView == null) {
			rowView = mInflater.inflate(R.layout.sale_list_item, null);
			rowView.setOnTouchListener(onItemTouchListener);
			viewCache = new SaleListItemViewCache(rowView);
			rowView.setTag(viewCache);
		} else {
			viewCache = (SaleListItemViewCache) rowView.getTag();
		}

		viewCache.getTextViewEntryNum().setText(String.valueOf(position + 1));
		viewCache.getTextViewQty().setText(String.valueOf(mData.getItem(position).getQty()));
		// viewCache.getTextViewName().setText(String.valueOf(mData.getItem(position).getName()));

		String sContent = mData.getItem(position).getDisplayContent() ;
		viewCache.getTextViewName().setText(sContent);

		
		viewCache.getTextViewPrice().setText("" + MoleQUtil.fixNumber(BigDecmialTool.round(mData.getItem(position).getPrice(),2)));
		viewCache.getButtonDelete().setTag(position);
		viewCache.getButtonDelete().setOnClickListener(mOnClickListener);
		viewCache.setDisplayControls(mIsShowControls);
		// viewCache.setSelected(false);

		if (position == mData.mSelectIndex) {
			viewCache.setSelected(true);
		} else {
			viewCache.setSelected(false);
		}
		viewCache.setPosition(position);

		return rowView;

	}
	
//	public void scrollToRow(int row) {
//
//		SaleListItemViewCache cache = null;
//		mContext
//		for (int i = 0; i < mListView.getChildCount(); i++) {
//
//			cache = (SaleListItemViewCache) mListView.getChildAt(i).getTag();
//			cache.setSelected(false);
//			if (row == cache.getPosition()) {
//				cache.setSelected(true);
//			}
//		}
//
//		mSelectIndex = row;
//
//		if (mSelectIndex == mData.getItemCount() - 1) {
//			mListView.setSelection(mSelectIndex);
//		}
//	}
	
//	public void scrollToRow(int row) {
//
//		SaleListItem item = null;
//		if(mSelectIndex >= 0){
//			item = mData.getItem(mSelectIndex);
//			item.setSelected(false);
//			updateView(mSelectIndex);
//		}
//
//		mSelectIndex = row;
//
//		item = mData.getItem(mSelectIndex);
//		item.setSelected(true);
//		updateView(mSelectIndex);
//		
//
//	}
	

	
	public void updateView(int itemIndex, boolean isShowControl) {
		// 得到第1个可显示控件的位置,记住是第1个可显示控件噢。而不是第1个控件
		int visiblePosition = mListView.getFirstVisiblePosition();
//		int showingRecordCnt = mListView.getChildCount();
//		int visibleEndPostion = visiblePosition + showingRecordCnt;
//		
//		Log.i("VIEW_Adater","start Postion: -> "+visiblePosition);
//		Log.i("VIEW_Adater","End Postion: -> "+visibleEndPostion);
//		
//		if(itemIndex < visiblePosition || itemIndex >= visibleEndPostion)
//			return;
//		
		
		// 得到你需要更新item的View
		View view = mListView.getChildAt(itemIndex - visiblePosition);
		
		
//		SaleListItem item = mData.getItem(itemIndex);
		
		SaleListItemViewCache cache = (SaleListItemViewCache) view.getTag();
		cache.setDisplayControls(isShowControl);
		

	}

//	public void updateView(int itemIndex) {
//		// 得到第1个可显示控件的位置,记住是第1个可显示控件噢。而不是第1个控件
//		int visiblePosition = mListView.getFirstVisiblePosition();
//		// 得到你需要更新item的View
//		int showingRecordCnt = mListView.getChildCount();
//		int visibleEndPostion = visiblePosition + showingRecordCnt;
//		if(itemIndex < visiblePosition || itemIndex >= visibleEndPostion)
//			return;
//		
//		View view = null;
//		SaleListItemViewCache cache = null;
//		
//		view = mListView.getChildAt(itemIndex - visiblePosition);
//		cache = (SaleListItemViewCache) view.getTag();
//		cache.setSelected(true);
//		
//
//	}
	
	public void setListView(ListView listView) {
		this.mListView = listView;
	}
	
	public void setSelectedRow(){
//		mSelectIndex = row;
//		notifyDataSetInvalidated();
		this.notifyDataSetInvalidated();
		
	}
	
	public void onReturnValue(int viewId, String parm){
		if (mContext instanceof OnResultListener) {
			OnResultListener linstener = (OnResultListener) this.mContext;
			linstener.onReturnValue( viewId, parm);
		}
	}
	
	private OnClickListener mOnClickListener = new OnClickListener() {

		public void onClick(View v) {
		
//			onReturnValue(v.getId(),String.valueOf(mSelectIndex));
			switch (v.getId()) {

			case R.id.buttonSaleItemDelete:
				onReturnValue(v.getId(),v.getTag().toString());
				// confirmDelete();

//				int deletePosition = (Integer) v.getTag();
//				setSelectedRow(deletePosition);
				// removeItem(deletePosition);
				break;
			}
		}

	};
	
	private OnTouchListener onItemTouchListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				SmartBeep.getInstance().play(SmartBeep.SOUND_CLICK);
			}
			return false;
		}

	};

	public void setShowControl(boolean bSwitch) {
		this.mIsShowControls = bSwitch;
//		this.notifyDataSetInvalidated();
		
		int visiblePosition = mListView.getFirstVisiblePosition();
		int showingRecordCnt = mListView.getChildCount();
		int visibleEndPostion = visiblePosition + showingRecordCnt;
		for (int i = visiblePosition; i < visibleEndPostion; i++) {
			
			updateView(i,bSwitch);
			
		}
		
	}

	public void setDataSource(SaleList lst) {
		// TODO Auto-generated method stub
		mData = lst;
	}

}